package com.bluesky.controller;

import com.bluesky.dto.lesson006.GoodsDetailResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("lesson006")
public class Lesson006Controller {


    @Autowired
    private ThreadPoolTaskExecutor threadPoolExecutor;

    /**
     * 根据商品id获取商品信息(基本信息、描述信息、评论量，收藏量)
     * @param goodsId
     * @return
     */
    @GetMapping("/getGoodsDetail")
    public GoodsDetailResponse getGoodsDetail(@RequestParam("goodsId") String goodsId) {
        Long st = System.currentTimeMillis();
        GoodsDetailResponse goodsDetailResponse = new GoodsDetailResponse();
        //1、获取商品基本信息，耗时100ms
        goodsDetailResponse.setGoodsInfo(getGoodsInfo(goodsId));
        //2、获取商品评论数量，耗时100ms
        goodsDetailResponse.setCommentCount(getGoodsCommentCount(goodsId));
        //3、获取商品描述，耗时100ms
        goodsDetailResponse.setGoodsDescription(getGoodsDescription(goodsId));
        //4、获取商品收藏量，耗时100ms
        goodsDetailResponse.setFavoriteCount(getGoodsFavoriteCount(goodsId));

        log.info("获取商品信息，普通版耗时："+(System.currentTimeMillis()-st)+"ms");
        return goodsDetailResponse;
    }

    /**
     * 优化后的方法，根据商品id获取商品信息(基本信息、描述信息、评论量，收藏量)
     * @param goodsId
     * @return
     */
     @GetMapping("/getGoodsDetailNew")
    public GoodsDetailResponse getGoodsDetailNew(@RequestParam("goodsId") String goodsId) {
        long st = System.currentTimeMillis();
        GoodsDetailResponse goodsDetailResponse = new GoodsDetailResponse();
        //1、获取商品基本信息，耗时100ms
        CompletableFuture<Void> goodsInfo = CompletableFuture.runAsync(()->{
            goodsDetailResponse.setGoodsInfo(getGoodsInfo(goodsId));
        },threadPoolExecutor);
        //2、获取商品评论数量，耗时100ms
        CompletableFuture<Void> goodsCommentCount = CompletableFuture.runAsync(()->
                goodsDetailResponse.setCommentCount(getGoodsCommentCount(goodsId)),threadPoolExecutor);
        //3、获取商品描述，耗时100ms
        CompletableFuture<Void> goodsDescription = CompletableFuture.runAsync(()->
                goodsDetailResponse.setGoodsDescription(getGoodsDescription(goodsId)),threadPoolExecutor);
        //4、获取商品收藏量，耗时100ms
        CompletableFuture<Void> goodsFavoriteCount = CompletableFuture.runAsync(()->
                goodsDetailResponse.setFavoriteCount(getGoodsFavoriteCount(goodsId)),threadPoolExecutor);
        //等待上面执行结束
        CompletableFuture.allOf(goodsInfo,goodsCommentCount,goodsDescription,goodsFavoriteCount).join();
        log.info("获取商品信息，新版耗时："+(System.currentTimeMillis()-st)+"ms");
        return goodsDetailResponse;
    }

    private int getGoodsFavoriteCount(String goodsId) {

        try{
            log.info("获取商品收藏量");
            TimeUnit.MILLISECONDS.sleep(100);
        }catch (InterruptedException e){
            Thread.currentThread().interrupt();
            throw  new RuntimeException(e);
        }
    	return 10000;
    }

    private int getGoodsCommentCount(String goodsId) {
        try{
            log.info("获取商品评论量");
            TimeUnit.MILLISECONDS.sleep(100);
        }catch (InterruptedException e){
            Thread.currentThread().interrupt();
            throw  new RuntimeException(e);
        }
        return 10000;
    }

    private String getGoodsDescription(String goodsId) {
        try{
            log.info("获取商品描述");
            TimeUnit.MILLISECONDS.sleep(100);
        }catch (InterruptedException e){
            Thread.currentThread().interrupt();
            throw  new RuntimeException(e);
        }
        return "商品描述信息 goods description";
    }

    private String getGoodsInfo(String goodsId) {
        try{
            log.info("获取商品信息");
            TimeUnit.MILLISECONDS.sleep(100);
        }catch (InterruptedException e){
            Thread.currentThread().interrupt();
            throw  new RuntimeException(e);
        }
        return "商品基本信息 goods info";
    }


}
